<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1400" destroy-on-close :close-on-press-escape="mode != 'show' ? false : true" @closed="$emit('closed')" align-center>
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="130" ref="dialogForm">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="收款单号" prop="collect_code">
                        <el-input v-model="form.collect_code" placeholder="收款单号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="客户" prop="customer_id">
                        <el-select v-model="form.customer_id" filterable clearable placeholder="请选择客户" style="width:100%" @change="clientChange">
                            <el-option v-for="item in clientList" :key="item.id" :label="item.customer_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="收款日期" prop="business_date">
                        <el-date-picker v-model="form.business_date" type="date" placeholder="请选择收款日期" style="width: 100%;" :value-format="'YYYY-MM-DD'"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="应收金额" prop="only_money">
                        <el-input v-model="only_money" placeholder="应收金额" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="18">
                <el-form-item label="结算信息" prop="detail">
                    <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="250" show-summary @sons="moneyInput">
                        <el-table-column label="外币" prop="foreign_currency" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.foreign_currency" placeholder="外币" @input="accInput"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="汇率" prop="exchange_rate" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.exchange_rate" placeholder="汇率" @input="accInput"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="收款金额" prop="money" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.money" placeholder="金额" @input="moneyInput"></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="备注" type="textarea"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="附件" prop="enclosure">
                    <sc-upload-file v-model="form.enclosure" :limit="1">
                        <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                    </sc-upload-file>
                </el-form-item>
            </el-col>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
    <collection v-if="dialog.detail" ref="detailDialog" @success="handleSuccess" @closed="dialog.detail = false" :query="{customer_id:this.form.customer_id}"></collection>
</template>

<script>
import Collection from './collection.vue'

export default {
    components: {
        Collection,
    },
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '新增',
                edit: '收款',
                show: '查看'
            },
            visible: false,
            isSaveing: false,
            dialog: {
                detail: false,
            },
            form: {
                business_date: this.moment().format('YYYY-MM-DD'),
                collect_code: "",
                customer_id: "",
                money: "",
                remark: "",
                enclosure: "",
                status: 1,
                accounts_all: [],
                detail: [],
            },
            addTemplate: {},
            accounts_all: {},
            clientList: [],     //客户
            order: "",
            only_money: "",
        }
    },
    mounted() {
        this.getClient()
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    // if (this.form.money > this.only_money) {
                    //     this.$alert("本次收款金额已超出应收金额，请检查", "提示", {type: 'error'})
                    // } else {
                        this.isSaveing = true;
                        var res = []
                        if(this.form.id){
                            res = await this.$API.finance.shouorder.edit.post(this.form);
                        }else{
                            res = await this.$API.finance.shouorder.add.post(this.form);
                        }
                        this.isSaveing = false;
                        if (res.code == 1) {
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success("操作成功")
                        } else {
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    // }
                }
            })
        },
        //表单注入数据
        async setData(data) {
            this.form.id = data.id
            var res = await this.$API.finance.shouorder.detail.get({id: data.id});
            this.form.customer_id = res.data.customer_id
            this.form.collect_code = res.data.collect_code
            this.form.business_date = res.data.business_date
            this.form.remark = res.data.remark
            this.form.enclosure = res.data.enclosure
            this.form.status = res.data.status
            this.form.money = res.data.money
            this.form.detail = res.data.detail
            let array = []
            res.data.accounts.map(item => {
                    array.push({
                    business_date: item.accounts.business_date,
                    contract_code: item.accounts.contract && item.accounts.contract.contract_code,
                    all_money: item.accounts.money,
                    only_money: item.accounts.only_money,
                    pay_money: item.accounts.pay_money,
                    money: item.money,
                    type: item.accounts.type,
                })
            })
            this.form.accounts_all = array
            this.clientChange(res.data.customer_id)
        },
        // 客户
        async getClient() {
            var res = await this.$API.client.client.all.get();
            this.clientList = res.data;
        },
        accInput() {
            let count_price = 0
            this.form.detail.map(item => {
                if (item.foreign_currency && item.exchange_rate) {
                    item.money = (Number(item.foreign_currency) * Number(item.exchange_rate)).toFixed(1)
                    count_price = Number(count_price) + Number(item.money)
                }
            })
            this.form.money = count_price
        },
        moneyInput(){
            let count_price = 0
            this.form.detail.map(item => {
                if(item.money){
                    count_price = Number(count_price) + Number(item.money)
                }
            })
            this.form.money = count_price
        },
        async clientChange(e){
            var res = await this.$API.finance.shouorder.custom.post({customer_id: e})
            this.only_money = res.data.all_only_money
        },
    }
}
</script>

<style>
</style>
